﻿using DTOs;
using RBAC.Domain.Service.Interfae;
using RBAC.Repository.Interfae;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.Domain.Service.Impl
{
    public class RoleService : IRoleService
    {
        /// <summary>
        /// 角色仓储
        /// </summary>
        protected readonly IRoleRepository _roleRepository;
        /// <summary>
        /// 用户仓储
        /// </summary>
        protected readonly IUserRepository _userRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleRepository"></param>
        public RoleService(IRoleRepository roleRepository,
            IUserRepository userRepository)
        {
            _roleRepository = roleRepository;
            _userRepository = userRepository;
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<int> AddRoles(Roles role)
        {
            //判断唯一性
            var addreol = await _roleRepository.GetAsync(d => d.roleName == role.roleName);
            if (addreol.Count() > 0)
            {
                return -1;
            }
       
            return await _roleRepository.AddAsync(role);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelRole(Roles roles)
        {
            var delRoles = await _roleRepository.GetModelsAsync(roles.roleId);
            delRoles.IsDel = true;
            delRoles.delName = "王器";
            delRoles.delDate = DateTime.Now;
            return await _roleRepository.UpdateAsync(delRoles);
        }
        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        public async Task<PageDto<Roles>> GetRoleAsync(int pageSize, int pageIndex)
        {
            PageDto<Roles> pageDto = new PageDto<Roles>();
            var listobj = from a in await _roleRepository.GetAllAsync()
                             where !a.IsDel
                             select a;
            pageDto.ToalCount = listobj.Count();
            pageDto.PageCount = (int)Math.Ceiling(listobj.Count() * 1.0 / pageSize);
            pageDto.list = listobj.OrderByDescending(d => d.addDate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageDto;


        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Roles> GetRoleByValues(Roles role)
        {
            return await _roleRepository.GetModelsAsync(role.roleId);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<int> UpdateRole(Roles role)
        {
            //var roleUpdate = await _roleRepository.GetModelsAsync(role.roleId);
            role.updateName = "王宏";
            role.updateDate = DateTime.Now;
            return await _roleRepository.UpdateAsync(role);
        }
        /// <summary>
        /// 查询部门下的员工
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<Menument>> GetUserRole(List<int> ids)
        {

            try
            {
                var resultData = await _userRepository.GetAllAsync();
                var userRoles = (from a in resultData
                                 where ids.Contains(a.departmentId)
                                 select new UserDto
                                 {
                                     userInfoId = a.userInfoId,
                                     userName = a.userName,
                                 }).ToList();

                var userRolesList = userRoles.Select(a => new Menument
                {
                    Id = a.userInfoId,
                    label = a.userName,
                    children = new List<Menument>()
                }).ToList();

                return userRolesList;

            }
            catch (Exception ee)
            {
                var result = ee.Message;
                throw;
            }
        }
        /// <summary>
        /// 反填（从Token中取角色ID）1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Roles> GetRoleAlls(int id)
        {
            return await _roleRepository.GetModelsAsync(id);
        }
    }
}
